package com.lolc.ihm.component;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JRadioButton;

import com.lolc.LoLAutoCounterManager;
import com.lolc.bean.Champion;
import com.lolc.bean.Lane;
import com.lolc.ihm.Constants;
import com.lolc.ihm.listener.AutoCounterListener;
import com.lolc.ihm.listener.SetMyTurnListener;

public class MyTeamPlayerBox extends PlayerBox{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JRadioButton myTurnRB;
	
	private JButton countBtn;
	
	public  MyTeamPlayerBox(int axis, int id) {
		super(axis,id);
	}
		
	public void initComponents(int id)
	{
		this.setBackground(Constants.COLOR_GRAY_1);
		this.setOpaque(true);

		initMyTurnRb(id,false);

		Champion selected=null;
		if(LoLAutoCounterManager.getInstance().getMyTeamSelections() !=null && LoLAutoCounterManager.getInstance().getMyTeamSelections()[id] !=null)
		{
			selected=LoLAutoCounterManager.getInstance().getMyTeamSelections()[id].getChampion();
		}		
		
		initchampionCB(id,Constants.GROUP_MY_TEAM_CHAMPIONS,selected);
		
		//Separator between the 2 lists
		Component spaceBefore = Box.createVerticalGlue();
		spaceBefore.setPreferredSize(new Dimension(Constants.VERTICAL_SEPARATOR_WIDTH, 0));
		this.add(spaceBefore);
		
		Lane selectedLane=null;
		if(LoLAutoCounterManager.getInstance().getMyTeamSelections() !=null && LoLAutoCounterManager.getInstance().getMyTeamSelections()[id] !=null)
		{
			selectedLane=LoLAutoCounterManager.getInstance().getMyTeamSelections()[id].getLane();
		}
		initLaneCB(id,Constants.GROUP_MY_TEAM_LANES,selectedLane);

		//Separator between the 2 lists
		Component spaceAfterLane = Box.createVerticalGlue();
		spaceAfterLane.setPreferredSize(new Dimension(Constants.VERTICAL_SEPARATOR_WIDTH, 0));
		this.add(spaceAfterLane);

		this.initClearBtn();

		//Separator between the 2 lists
		Component spaceAfter = Box.createVerticalGlue();
		spaceAfter.setPreferredSize(new Dimension(Constants.VERTICAL_SEPARATOR_WIDTH/2, 0));
		this.add(spaceAfter);		
		
		this.validate();
		this.repaint();
	}

	/**
	 * Initializes the radio button
	 * @param id
	 * @param selected
	 */
	public void initMyTurnRb(int id, boolean selected){
		//Radio Button
		myTurnRB= new JRadioButton("");
		this.add(myTurnRB);
		
		myTurnRB.setBackground(Constants.COLOR_GRAY_1);
		myTurnRB.addActionListener(new SetMyTurnListener(id));
		this.myTurnRB.setSelected(selected);
		myTurnRB.validate();
	}
	
	/**
	 * Initializes the button "Count"
	 * @param x
	 * @param y
	 */
	public void initCountBtn(int x, int y)
	{
		countBtn = new JButton(Constants.BUTTON_COUNT_TEXT);	
		countBtn.setPreferredSize(new Dimension(x, y));
		countBtn.validate();
		countBtn.addActionListener(new AutoCounterListener());		
		this.add(countBtn);		
	}
	
	/**
	 * Update the row for "my turn"
	 * @param id
	 */
	public void initMyTurnComponent(int id)
	{
		
		//Get the size of the champion combobox before remove it
		int btnWidth=this.championCB.getSize().width;
		
		this.removeAll();		
		this.initMyTurnRb(id,true);
		
		this.initCountBtn(btnWidth,Constants.COMBOBOX_HEIGHT);
		
		//Separator between the 2 lists
		Component spaceBefore = Box.createVerticalGlue();
		spaceBefore.setPreferredSize(new Dimension(Constants.VERTICAL_SEPARATOR_WIDTH, 0));
		this.add(spaceBefore);
		
		Lane selectedLane=null;
		if(LoLAutoCounterManager.getInstance().getMyTeamSelections() !=null && LoLAutoCounterManager.getInstance().getMyTeamSelections()[id] !=null)
		{
			selectedLane=LoLAutoCounterManager.getInstance().getMyTeamSelections()[id].getLane();
		}
		this.initLaneCB(id,Constants.GROUP_MY_PLAYER_LANE,selectedLane);
				
		//Separator between the 2 lists
		Component spaceAfter = Box.createVerticalGlue();
		spaceAfter.setPreferredSize(new Dimension(Constants.VERTICAL_SEPARATOR_WIDTH, 0));
		this.add(spaceAfter);
		
		this.initClearBtn();
		
		//Separator between the 2 lists
		Component spaceAfterBtn = Box.createVerticalGlue();
		spaceAfterBtn.setPreferredSize(new Dimension(Constants.VERTICAL_SEPARATOR_WIDTH/2, 0));
		this.add(spaceAfterBtn);
		
		this.validate();
		this.repaint();
	}
			
	/**
	 * @return the myTurnRB
	 */
	public JRadioButton getMyTurnRB() {
		return myTurnRB;
	}

	/**
	 * @param myTurnRB the myTurnRB to set
	 */
	public void setMyTurnRB(JRadioButton myTurnRB) {
		this.myTurnRB = myTurnRB;
	}

}
